var app = getApp()
// var bmap = require('../../../utils/bmap-wx.min.js');
let __test_account__, __test_psword__;
let disp = require("../../../utils/broadcast");
var tcity = require("../../../utils/citys.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dex: '',
    page: 0,
    zhicheng: '',
    keyword: '',
    page: 0,
    parentid: "",
    doctor: [],
    isLoading: !0,
    code: '',
    hint: true,
    name: "",
    tab_state: 1,
    psd: "123",
    f_id:"",
    f_title:"",
    grant_type: "password",
    colorArr: ["#f8b551", "#ea68a2", "#80c269", "#00b7ee"],
    bigMask: false,
    maskshow: false,
    maskCont: false,
    firstmask: true,
    hospital: [],
    pages: 0,
    rew: [],
    dex2: 0,
    pagesizes: 10,
    zhe: false,
    neworder: [],
    cityList: [{
      name: '全国',
      id: 0
    },
    {
      name: '太原',
      id: 0
    },
    {
      name: '大同',
      id: 0
    },
    {
      name: '晋中',
      id: 0
    },
    {
      name: '北京',
      id: 0
    },
    {
      name: '上海',
      id: 0
    },
    ],
    cityName: '',
    recommendList: ['综合推荐', '回答次数', '星级评分', '响应时间'],
    recommendLists: ['综合推荐', '离我最近', '预约量', '好评率'],
    recommend_state1: 0,
    recommend_state1s: 0,
    recommend_states: false,
    recommend_state: false,
    screen_states: false,
    screen_state: false,
    level: "",
    tabindex: 0,
    rolldistance: 'item_0',
    fuwu_list: [],
    keshi: "",
    tablist: [
      {
        title:"血压",
        type:"blood_pressure"
      },{
        title:"血糖",
        type:"blood_sugar"
      },{
        title:"尿酸",
        type:"uric_acid"
      },{
        title:"体温",
        type:"temperature"
      },{
        title:"心率",
        type:"heart_rate"
      }],
    tabIndex: 0,
    animationData: {},//圆形进图条动画
    j_id: "",
    userjiaren: "",
    userdata: "",
    follow: "",
    f_id: "",
    sflist:[
      {title:"11"},
      {title:"22"},
      {title:"33"},
    ]
  },
  fs_choose:function(e){
    var that = this
    var follow_list=that.data.follow_list;
    var index=e.detail.value;
    console.log(index)
    var f_title = follow_list[index].title;
    that.setData({
      f_title:f_title,
      f_id:follow_list[index].id
    })
    that.getUserFollow();
    // sflist[index].title
  },
  // 选择一级科室
  keshi_yiji: function (e) {
    console.log(e)
    var dex = e.currentTarget.dataset.dex;
    this.setData({
      keshi_yiji_dex: dex,
      keshi_one: e.currentTarget.dataset.id
    })
    this.getKeshi_two();
  },
  vips: function () {
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/vip/vip',
    })
  },
  // 选择二级科室
  keshi_erji: function (e) {
    console.log(e)
    var dex = e.currentTarget.dataset.dex;
    this.setData({
      keshi_erji_dex: dex,
      parentid: e.currentTarget.dataset.id,
      page: 0,
      pages: 0,
      list: []
    })

    if (this.data.tab_state == 1) {
      this.getZhuanjia();
    } else {
      this.getHospital();
    }
    this.close_screen()
  },
  bindtabs(e){
    console.log(e)
    this.setData({
      tabIndex:e.target.dataset.index,
      type:e.currentTarget.dataset.type,
    })
  },
  // tab切换
  city_choose(e) {
    console.log(e)
    this.setData({
      city_state: !this.data.city_state,
      screen_zhe: !this.data.city_state,
      recommend_state: false,
      screen_state: false,
      keshi_state: false,
    })
  },
  keshi_choose(e) {
    console.log(e)
    this.setData({
      keshi_state: !this.data.keshi_state,
      screen_zhe: !this.data.keshi_state,
      city_state: false,
      recommend_state: false,
      screen_state: false,
    })
  },
  // 查看全部记录
  bindhistory:function(){
    var that = this;
    var type = that.data.type;
    if(type == 'blood_pressure')
    {
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/bloodpressure_report/bloodpressure_report?openid='+wx.getStorageSync('openid')+'&j_id='+that.data.j_id,
      })
    }else if(type == 'blood_sugar')
    {
      wx.navigateTo({
        url: '/hyb_yl/zhuanjiasubpages/pages/bloodsugar_report/bloodsugar_report?openid='+wx.getStorageSync('openid')+'&j_id='+that.data.j_id,
      })
    }else if(type == 'uric_acid')
    {
      wx.navigateTo({
        url: '/hyb_yl/pharmacy/pages/uric_acid/uric_acid?openid='+wx.getStorageSync('openid')+'&j_id='+that.data.j_id,
      })
    }else if(type == 'temperature')
    {
      wx.navigateTo({
        url: '/hyb_yl/pharmacy/pages/temperature/temperature?openid='+wx.getStorageSync('openid')+'&j_id='+that.data.j_id,
      })
    }else if(type == 'heart_rate')
    {
      wx.navigateTo({
        url: '/hyb_yl/pharmacy/pages/heart_rate_cont/heart_rate_cont?openid='+wx.getStorageSync('openid')+'&j_id='+that.data.j_id,
      })
    }
  },
  // 查看随访详情
  bindplan:function(e){
    var that = this;
    wx.navigateTo({
      url: '/hyb_yl/huanzhe/pages/plan_detail/plan_detail?id='+e.currentTarget.dataset.id,
    })
  },
  // 查看随访信息详情
  // follow_detail:function(e){
  //   var id = e.currentTarget.dataset.id;
  //   var type = e.currentTarget.dataset.type;
  //   if(type == 'article')
  //   {
  //     wx.navigateTo({
  //       url: '/hyb_yl/userLife/pages/dynamicDetails/dynamicDetails?a_id='+id,
  //     })
  //   }else if(type == 'answer')
  //   {
  //     wx.navigateTo({
  //       url: '/hyb_yl/huanzhe/pages/questionnaire/questionnaire?id='+id,
  //     })
  //   }
  // },
  goHospitalList: function () {
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/hospitalList/hospitalList',
    })
  },
  tanchu: function () {
    wx.navigateTo({
      url: '/hyb_yl/sgin/sgin',
    })
  },
  jiankang: function () {
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=1&gren=1',
    })
  },
  cityChose: function (e) {
    console.log(e)
    var that = this
    var addressId = this.data.cityList[e.detail.value].parentid
    var addressname = this.data.cityList[e.detail.value].name
    console.log(addressname)
    app.globalData.addressId = addressId
    app.globalData.addressname = addressname
    that.setData({
      cityName: this.data.cityList[e.detail.value].name,
      addressId: addressId,

    })
    var tab_state = that.data.tab_state;
    if (tab_state == 1) {
      that.setData({
        page: 0
      })
      that.getZhuanjia()
    } else if (tab_state == 2) {
      that.setData({
        pages: 0
      })
      console.log('111')
      that.getHospital()
    }
  },
  // 医生说更多
  doctrospeack: function () {
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/doctrospeack/doctrospeack',
    })
  },
  // 医说详情
  speackdetail: function (e) {
    //console.log(e)
    var index = e.detail
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunanlixq/zixunanlixq',
    })
  },
  searchbtn() {
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/search/search',
    })
  },
  //我的医说
  mesaid() {
    wx.navigateTo({
      url: '/hyb_yl/mycenter/pages/myMedical/myMedical',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    setTimeout(() => {
      var animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease',
      })
      this.animation = animation
      animation.rotate(3 / 6 * 360).step()    //圆形进度占比
      this.setData({
        animationData: animation.export()
      })
    }, 1000)
    if (options.scene) {
      var info = decodeURIComponent(options.scene)
      //上级代理id
      var tkid = info.split("id=")[1];
      app.util.request({
        'url': 'entry/wxapp/index.addptuser',
        data: {
          tkid: tkid,
          openid: wx.getStorageSync('openid')
        },
        success: function (res) {
          console.log(res)
        }
      })
      app.globalData.tkid = tkid
      this.setData({
        tkid: tkid
      })
    }
    // wx.getLocation({
    //   type: 'wgs84',
    //   success: function (res) {
    //     that.setData({
    //       longitude: res.longitude,
    //       latitude: res.latitude
    //     })
    //     console.log(res);
    //     wx.setStorageSync("latitude", res.latitude);
    //     wx.setStorageSync("longitude", res.longitude);
    //     var addressId = 0
    //     that.getHospital(addressId);

    //   }
    // })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    // 获取类型
    // app.util.request({
    //   url: "entry/wxapp/index.serverpxlist",
    //   success: function (resing) {
    //     console.log(resing)
    //     that.setData({
    //       types: resing.data
    //     })
    //   }
    // })
    // // 获取医院等级
    // app.util.request({
    //   url: "entry/wxapp/index.hospital_level",
    //   success: function (resing) {
    //     that.setData({
    //       level_arr: resing.data
    //     })
    //   }
    // })
    // // 获取科室
    // app.util.request({
    //   url: "entry/wxapp/index.address",
    //   data: {
    //     op: 'keshi'
    //   },
    //   success: function (resing) {
    //     console.log(resing)
    //     that.setData({
    //       ktypes: resing.data
    //     })
    //   }
    // })
    // // 获取职称列表
    // app.util.request({
    //   'url': 'entry/wxapp/zhuanjia.getzhichenglist',
    //   success: function (res) {
    //     console.log(res)
    //     that.setData({
    //       section2: res.data
    //     })
    //   }
    // })
    // 获取首页头部
    app.util.request({
      'url': "entry/wxapp/index.getheader",
      success: function (res) {
        that.setData({
          header_list: res.data
        })
      }
    })

    app.util.request({
      'url': "entry/wxapp/follow.get_over",
      success: function (res) {
        that.setData({
          over_list: res.data
        })
      }
    })
    //查询是否是拓客专员
    app.util.request({
      'url': 'entry/wxapp/index.iftuoke',
      data: {
        openid: wx.getStorageSync('openid'),
        tkid: tkid
      },
      success: function (res) {
        console.log(res, app.globalData.tkid)
        if (res.data) {
          console.log('2')
          if (res.data.tkid == 0) {
            app.globalData.tkid = res.data.id //如果自己是自然进入的那么自己就是一级推客
            app.globalData.ziji = 1 //如果自己是1
          } else {
            app.globalData.tkid = res.data.tkid //一级分销商
            app.globalData.ziji = 0 //如果不是自己是0
          }
          //查询我上级的上级
          app.util.request({
            url: 'entry/wxapp/tuike.myshangji',
            data: {
              id: res.data.tkid
            },
            success: function (res) {
              console.log(res)
              if (res.data == 0) {
                app.globalData.mytkid = '' //二级分销商
              } else {
                app.globalData.mytkid = res.data //上级id
              }
            }
          })
        }
      },
    })
    // app.util.request({
    //   'url': 'entry/wxapp/zixun.getalluser',
    //   success: function (res) {
    //     that.setData({
    //       rew: res.data
    //     })
    //   },
    //   fail: function (err) {
    //     console.log(err)
    //   },
    // })
    // 获取绿通项目
    that.getGreen();
    
    app.util.request({
      'url': 'entry/wxapp/index.neworder',
      data: {
        type: 'index'
      },
      success: function (res) {
        that.setData({
          neworder: res.data
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
    app.util.request({
      'url': 'entry/wxapp/index.techen',
      success: function (res) {
        var msg = res.data.wxapp_mb
        that.setData({
          active: msg.refundNotice, //就诊提醒

        })
        var tencent_sdkappid = parseInt(res.data.tencent_sdkappid)
        var tencent_key = res.data.tencent_key
        wx.setStorageSync('sdkappid', tencent_sdkappid)
        wx.setStorageSync('secretkey', tencent_key)
      },
    })
    var me = this
    this.getBase()
    this.getfooter()
    this.getHotAnswer()
    this.getAddress()
    // 热门问题图片
    // 获取首页幻灯片及热门问题图片
    this.getAdv();
    that.getAilist();
    // me.loadInfo();
    // var baidukey = wx.getStorageSync('baidukey')
    // var BMap = new bmap.BMapWX({
    //   ak: baidukey
    // });

    //记录登记时间（每天记录一次）
    app.util.request({
      url: "entry/wxapp/authorize.logintime",
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {

      }
    })
    setTimeout(function () {
      me.setData({
        hint: false
      })
    }, 3000)


    if (options.code) {
      me.setData({
        code: options.code
      })
    }
    this.getKeshi_one()
    this.getHotcity()
    this.cityitem()
    this.provinceitem()
  },
  toask:function(){
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/chat_list/chat_list?zid=' + this.data.follow.zid + '&openid=' + wx.getStorageSync('openid') + '&role=0&title='+this.data.follow.z_name,
    })
  },
  getUser: function () {
    var that = this;
    var j_id = that.data.j_id;
    if(j_id == '')
    {
      app.util.request({
        'url': "entry/wxapp/follow.Userone",
        data: {
          openid: wx.getStorageSync('openid'),
        },
        success: function (res) {
          that.setData({
            userjiaren: res.data,
            j_id: res.data.j_id
          })
          // 获取用户测量记录
          that.getUserData();
          that.getAllFollow()
          // 获取用户最后一次随访
          that.getUserFollow()
        }
      })
    }else{
      app.util.request({
        'url': "entry/wxapp/follow.userjiaren",
        data: {
          openid: wx.getStorageSync('openid'),
          j_id:j_id
        },
        success: function (res) {
          that.setData({
            userjiaren: res.data,
          })
          // 获取用户测量记录
          that.getUserData();
          that.getAllFollow()
          // 获取用户最后一次随访
          that.getUserFollow()
        }
      })
    }
    
  },
  
  // 获取用户测量记录
  getUserData: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/follow.userData",
      data: {
        openid: wx.getStorageSync('openid'),
        j_id: that.data.j_id
      },
      success: function (res) {
        that.setData({
          userdata: res.data
        })
      }
    })
  },
  getAllFollow:function(){
    var that = this;
    app.util.request({
      'url': "entry/wxapp/follow.all_follow",
      data: {
        openid: wx.getStorageSync('openid'),
        j_id: that.data.j_id,
      },
      success: function (res) {
        that.setData({
          follow_list: res.data,
        })
      }
    })
  },
  // 获取用户最后一次随访
  getUserFollow: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/follow.last_follow",
      data: {
        openid: wx.getStorageSync('openid'),
        j_id: that.data.j_id,
        id:that.data.f_id
      },
      success: function (res) {
        that.setData({
          follow: res.data,
          f_id: res.data.f_id,
          f_title:res.data.follows[0].title
        })
      }
    })
  },
  getAilist: function (res) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/index.ai_list",
      success: function (res) {
        that.setData({
          ai_list: res.data
        })
        console.log(that.data.ai_list);
      }
    })
  },
  getGreen: function (res) {
    var that = this;
    app.util.request({
      url: "entry/wxapp/green.index",
      success: function (res) {
        that.setData({
          green_list: res.data
        })
      }
    })
  },

  // 绿通详情
  togreen: function (e) {
    var that = this;

    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var keyword = e.currentTarget.dataset.keyword;
    var url = e.currentTarget.dataset.url;
    if (keyword == 'shuhoukangfu') {
      wx.navigateTo({
        url: '/hyb_yl/krecovery/pages/after_operation/after_operation' + '?id=' + id + '&title=' + title + '&keyword=' + keyword + '&ser_key=' + keyword + '&name=' + title,
      })
    } else {
      wx.navigateTo({
        url: url + '?id=' + id + '&title=' + title + '&keyword=' + keyword + '&ser_key=' + keyword + '&name=' + title,
      })
    }


  },
  // 获取附近诊所
  getHospital: function (addressId) {
    var that = this;
    if (addressId == 'undefined') {
      var addressId = 0
    }
    if (addressId !== 0) {
      app.util.request({
        'url': "entry/wxapp/index.near_hospital",
        data: {
          addressId: addressId,
          latitude: wx.getStorageSync("latitude"),
          longitude: wx.getStorageSync("longitude"),
          dex: that.data.dex2,
          city: that.data.cityName,
          level: that.data.level,
          page: that.data.pages,
          pagesizes: that.data.pagesizes,
        },
        success: function (res) {
          var pages = that.data.pages + 1;
          if (that.data.pages == 0) {
            that.setData({
              hospital: res.data
            })
          } else {
            that.setData({
              hospital: that.data.hospital.concat(res.data)
            })
          }
          that.setData({
            pages: pages,
          })

        }
      })

    } else {
      app.util.request({
        'url': "entry/wxapp/index.near_hospital",
        data: {
          addressId: addressId,
          latitude: wx.getStorageSync("latitude"),
          longitude: wx.getStorageSync("longitude"),
          page: that.data.pages,
          dex: that.data.dex2,
          city: that.data.cityName,
          level: that.data.level,
          pagesizes: that.data.pagesizes,
        },
        success: function (res) {

          var pages = that.data.pages + 1;
          if (that.data.pages == 0) {
            that.setData({
              hospital: res.data
            })
          } else {
            that.setData({
              hospital: that.data.hospital.concat(res.data)
            })
          }
          that.setData({
            pages: pages,
          })
        }
      })
    }


  },
  goback: function (e) {
    wx.navigateTo({
      url: '/hyb_yl/mycenter/pages/mechanism/mechanism?hid=' + e.currentTarget.dataset.hid,
    })
  },
  // 获取首页幻灯片及热门问题图片
  getAdv: function () {
    var that = this;
    // 头部幻灯片
    app.util.request({
      url: "entry/wxapp/index.adv",
      data: {
        position: 0
      },
      success: function (res) {
        //console.log(res.data)
        that.setData({
          huandengpian: res.data
        })
      }
    })
    // 热门问题图片
    app.util.request({
      url: "entry/wxapp/index.adv",
      data: {
        position: 1
      },
      success: function (res) {

        that.setData({
          hot_adv: res.data
        })
      }
    })

  },
  //查询导航
  getfooter() {
    var that = this
    app.util.request({
      url: "entry/wxapp/index.footerlist",
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (resing) {
        //console.log(resing)
        let footers = {
          footerTab: 0,
          footerlist: resing.data,
        };
        that.setData({
          footer: footers
        })

      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    this.getMenu()
    this.getLmhosp()
    this.getFenlei()
    this.getTeserver()
    this.getYishuo()
  },
  // 切换患者
  bindswitch:function(){
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=index',
    })
  },
  // 去登录
  tologin:function(){
    wx.navigateTo({
      url: '/hyb_yl/sgin/sgin',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.isLock = false
    var parentid = this.data.parentid;
    var keshi = this.data.keshi;
    this.setData({
      page: 0,
      doctor: [],
      f_id:"",
      isLoading: !0
    })
    var openid = wx.getStorageSync('openid');
    if (openid != '') {
      this.getUser();
    }else{
      this.getUser();
    }
    
    this.getZhuanjia();
    var me = this
    me.getBase();
    var code = me.data.code
    var city = wx.getStorageSync('city')
    me.setData({
      position: city
    })
    var openid = wx.getStorageSync('openid')
    me.setData({
      openid:openid
    })
    if (openid) {
      me.setData({
        bigMask: true,
        maskshow: true,
        maskCont: false,
        firstmask: false,
      })
      app.util.request({
        'url': "entry/wxapp/user.usertip",
        data: {
          openid: openid
        },
        success: function (res) {
          console.log(1111)
          if (res.data == 0) {
            me.setData({
              zhe: true
            })
          }
        }
      })
      if (code == '') {
        //获取公众号授权
        // wx.navigateTo({
        //   url: '/hyb_yl/zhuanjiasubpages/pages/webview/webview',
        // })
      }

    } else {
      // if(me.data.maskshow==false){
      //   wx.navigateTo({
      //     url: '/hyb_yl/sgin/sgin',
      //   })
      // }
      me.setData({
        bigMask: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getBase()
    this.getMenu()
    this.getLmhosp()
    this.getFenlei()
  },
  addNum2: function (e) {
    var that = this
    var active = that.data.active
    wx.requestSubscribeMessage({
      tmplIds: [active],
      success: (res) => {

        //调用授权
        //调用授权
        let item = res[active]
        if (item == "accept") {
          app.util.request({
            'url': "entry/wxapp/user.changetip",
            data: {
              openid: wx.getStorageSync('openid'),
            },
            success: function (res) {
              that.setData({
                zhe: false
              })
            }
          })
        }

      },
      fail: function (res) {
        that.setData({

        })
      }
    })


  },
  /**
   * 页面上拉触底事件的处理函数
   */
  todetail: function (e) {
    var zid = e.currentTarget.dataset.zid
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid=' + zid,
    })
  },
  onReachBottom: function () {
    var tab_state = this.data.tab_state;
    if (tab_state == 1) {
      this.getZhuanjia();
    } else {
      this.getHospital();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //基础设置
  getBase: function () {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        console.log(res);
        var se = res.data.ztcolor
        var baidukey = res.data.baidukey
        var bastitle = res.data.show_title
        wx.setStorageSync('bastitle', bastitle)
        wx.setStorageSync('baidukey', baidukey)
        that.setData({
          baseinfo: res.data,
        })
        wx.setNavigationBarTitle({
          title: res.data.show_title
        })
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: se,
        })
        that.setData({
          bgc: se
        })
        wx.setStorage({
          key: 'color',
          data: se,
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
  },
  //菜单
  getMenu() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.menu',
      success: function (res) {
        console.log(res)
        let menuArr = res.data
        let Length = menuArr.length;
        that.setData({
          ['menuArr[' + Length + ']']: res.data,
        })
        console.log(that.data.menuArr)

      }
    })
    app.util.request({
      'url': 'entry/wxapp/index.menus',
      success: function (res) {
        console.log(res)
        let menuArr = res.data
        let Length = menuArr.length;
        that.setData({
          ['menuArrs[' + Length + ']']: res.data,
        })
        console.log(that.data.menuArr)

      }
    })
  },

  //特色服务
  getTeserver() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.teserver',
      success: function (res) {
        //console.log(res)
        let teserver = res.data
        let Length = teserver.length;
        var index = 0;
        for (var i = 0; i < teserver.length; i++) {

          if (teserver[i].pinyin == 'shoushukuaiyue') {
            var teserver1 = teserver[i].button = '立即预约'
          }
          if (teserver[i].pinyin == 'tijianjiedu') {
            var teserver1 = teserver[i].button = '立即解读'
          }
          if (teserver[i].pinyin == 'zhuanjiatuandui') {
            var teserver1 = teserver[i].button = '签约医生'
          }
          if (teserver[i].pinyin == 'yuanchengguahao') {
            var teserver1 = teserver[i].button = '立即挂号'
          }
          if (teserver[i].pinyin == 'yuyuejiuzhen' || teserver[i].pinyin == 'zhuyuananpai' || teserver[i].pinyin == 'shoushuanpai' || teserver[i].pinyin == 'baogaojiaji') {
            var teserver1 = teserver[i].button = teserver[i]['name']
          }
        }
        that.setData({
          ['teserver[' + Length + ']']: res.data,
        })
        //console.log(teserver)
      }
    })
  },
  // 绿通服务
  yuyuejiuzhen: function (e) {
    var that = this;
    var urls = e.currentTarget.dataset.url + '?ser_key=' + e.currentTarget.dataset.key + '&name=' + e.currentTarget.dataset.name
    wx.navigateTo({
      url: urls,
    })
  },
  zhuyuananpai: function (e) {
    var that = this;
    var urls = e.currentTarget.dataset.url + '?ser_key=' + e.currentTarget.dataset.key + '&name=' + e.currentTarget.dataset.name
    wx.navigateTo({
      url: urls,
    })
  },
  shoushuanpai: function (e) {
    var that = this;
    var urls = e.currentTarget.dataset.url + '?ser_key=' + e.currentTarget.dataset.key + '&name=' + e.currentTarget.dataset.name
    wx.navigateTo({
      url: urls,
    })
  },
  baogaojiaji: function (e) {
    var that = this;
    var urls = e.currentTarget.dataset.url + '?ser_key=' + e.currentTarget.dataset.key + '&name=' + e.currentTarget.dataset.name
    wx.navigateTo({
      url: urls,
    })
  },
  //联盟医院
  getLmhosp() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.cooperative_hospital',
      success: function (res) {

        let hospitaArr = res.data
        let Length = hospitaArr.length;
        that.setData({
          ['hospitaArr[' + Length + ']']: res.data,
        })
      }
    })
  },

  //合作医院详情
  hzyyxq(e) {
    var that = this
    var title = e.currentTarget.dataset.title
    var hid = e.currentTarget.dataset.id
    var uid = e.currentTarget.dataset.uid
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/specialtyContent/specialtyContent?hid=' + hid + '&title=' + title + '&uid=' + uid,
    })
  },

  //专病服务
  getFenlei() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.hotactor',
      success: function (res) {
        console.log("今日推荐", res.data)
        let fenleiArr = res.data
        let Length = fenleiArr.length;
        that.setData({
          ['fenleiArr[' + Length + ']']: res.data,
        })
      }
    })
  },
  buttonsites(e) {
    //console.log(e)
    var that = this
    var title = e.currentTarget.dataset.title
    var link = e.currentTarget.dataset.link
    var id = e.currentTarget.dataset.id
    var ser_key = e.currentTarget.dataset.ser_key

    if (link == "/hyb_yl/backstageServices/pages/vip/vip") {
      wx.navigateTo({
        url: '/hyb_yl/backstageServices/pages/vip/vip?ifzy=0',
      })
    } else if (link == "/hyb_yl/backstageServices/pages/yuanchengkaifang/yuanchengkaifang") {
      wx.navigateTo({
        url: '/hyb_yl/backstageServices/pages/yuanchengkaifang/yuanchengkaifang?ifzy=0',
      })
    } else if (link == '/hyb_yl/tabBar/jibingyufang/jibingyufang') {

      wx.reLaunch({
        url: '/hyb_yl/tabBar/jibingyufang/jibingyufang?index=1?ifzy=0',
      })
    } else if (link == '/hyb_yl/mycenter/pages/followDoc/followDoc') {

      wx.navigateTo({
        url: '/hyb_yl/mycenter/pages/followDoc/followDoc?typs=siren_doc?ifzy=0',
      })
    } else if (link == '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi' && wx.getStorageSync('openid') == '') {
      wx.showModal({
        cancelColor: '提示',
        content: '请先登录',
        success: function (con) {
          if (con.confirm) {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          } else {
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    } else if (ser_key == 'shuhoukangfu') {
      wx.navigateTo({
        url: '/hyb_yl/krecovery/pages/after_operation/after_operation' + '?id=' + id + '&title=' + title + '&keyword=' + ser_key + '&ser_key=' + ser_key + '&name=' + title,
      })
    } else {
      wx.navigateTo({
        url: link + '?tit=' + title + '&ser_key=' + ser_key + '&id=' + id + '&ifzy=0' + '&key_words=' + ser_key + '&title=' + title + '&keyword=' + ser_key + '&name=' + title + '&gren=1&type=1',
      })
    }
  },
  //按钮跳转
  buttonsite(e) {
    //console.log(e)
    var that = this
    var title = e.detail.value.title
    var link = e.detail.value.link
    //tit=电话急诊&ser_key=dianhuajizhen&id=13
    var id = e.detail.value.id
    var ser_key = e.detail.value.ser_key

    if (link == "/hyb_yl/backstageServices/pages/vip/vip") {
      wx.navigateTo({
        url: '/hyb_yl/backstageServices/pages/vip/vip?ifzy=0',
      })
    } else if (link == "/hyb_yl/backstageServices/pages/yuanchengkaifang/yuanchengkaifang") {
      wx.navigateTo({
        url: '/hyb_yl/backstageServices/pages/yuanchengkaifang/yuanchengkaifang?ifzy=0',
      })
    } else if (link == '/hyb_yl/tabBar/jibingyufang/jibingyufang') {

      wx.reLaunch({
        url: '/hyb_yl/tabBar/jibingyufang/jibingyufang?index=1?ifzy=0',
      })
    } else if (link == '/hyb_yl/mycenter/pages/followDoc/followDoc') {

      wx.navigateTo({
        url: '/hyb_yl/mycenter/pages/followDoc/followDoc?typs=siren_doc?ifzy=0',
      })
    } else if (link == '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi' && wx.getStorageSync('openid') == '') {
      wx.showModal({
        cancelColor: '提示',
        content: '请先登录',
        success: function (con) {
          if (con.confirm) {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          } else {
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    } else if(ser_key == 'follow'){
      that.getqianyue();
    }else {
      wx.navigateTo({
        url: link + '?tit=' + title + '&ser_key=' + ser_key + '&id=' + id + '&ifzy=0' + '&key_words=' + ser_key + '&type=1',
      })
    }
  },
  getqianyue:function(){
    var that = this;
    var openid = wx.getStorageSync('openid')
    if(openid == '')
    {
      wx.showModal({
        cancelColor: '提示',
        content:'请先登录',
        success:function(con)
        {
          if(con.confirm)
          {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          }else{
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    }else{
      app.util.request({
        'url':"entry/wxapp/green.getqylistser",
        data:{
          openid:wx.getStorageSync('openid')
        },
        success:function(res){
          var list = res.data.list;
          var j_id = res.data.j_id;
          if(j_id == false || j_id == 'false')
          {
            wx.showModal({
              title:"提示",
              showCancel:false,
              content: '请先添加患者信息',
              success:function(con)
              {
                if(con.confirm)
                {
                  wx.navigateTo({
                    url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=1&gren=1',
                  })
                }
              }
            })
          }else if(list.length == 0)
          {
            wx.showModal({
              title: '提示',
              content:"您暂无签约医生，请扫码签约",
              showCancel:false,
              success:function(con)
              {
                if(con.confirm)
                {
                  wx.scanCode({
                    success:function(re){
                      var data = re.path;
                      
                      var res = data.split("=")
                      
                      if(res.length > 2)
                      {
                        var zids = res[2].split("&");
                        var zid = zids[0]
                      }else if(res.length == 2)
                      {
                        var zid = res[1].split("%3D");
                        if(zid[1].indexOf("%26") >= 0)
                        {
                          zid = zid[1].split("%26");
                          zid = zid[0];
                        }else{
                          var zid = zid[1]
                        }
                        
                      }else{
                        var zids = res[1].split("%26")
                        var zidss = zids[0].split("%3D");
                        var zid = zidss[1]
                        var zid = res[1];
                        zid = zid.substr(6)
                      }
                      app.util.request({
                        'url': 'entry/wxapp/zhuanjia.checkcollect',
                        data: {
                          openid: wx.getStorageSync('openid'),
                          zid: zid,
                          cerated_type: 7, //签约专家
                        },
                        success: function (res) {
                          
                          if(res.data.ifqianyue == 1)
                          {
                            wx.showToast({
                              title: '签约中，请等待审核',
                            })
                          }else if(res.data.ifqianyue == 2)
                          {
                            wx.navigateTo({
                              url: '/hyb_yl/huanzhe/pages/private_services/private_services?zid='+zid+'&id='+that.data.id+'&title='+that.data.title+'&keyword='+that.data.keyword,
                            })
                          }else if(res.data.ifqianyue == 3 || res.data.ifqianyue == 4 || res.data.ifqianyue == 5 || res.data.ifqianyue == 0)
                          {
                            wx.navigateTo({
                              url: '/hyb_yl/huanzhe/pages/patient_report/patient_report?zid='+zid,
                            })
                          }else{
                            wx.navigateTo({
                              url: '/hyb_yl/huanzhe/pages/patient_report/patient_report?zid='+zid,
                            })
                          }
                          
                        },
                      })
                    }
                  })
                }
              }
            })
          }else{
            wx.showModal({
              title:"提示",
              content: '是否选择面签医生会诊',
              success:function(con)
              {
                if(con.confirm)
                {
                  wx.scanCode({
                    success:function(re){
                      var data = re.path;
                      
                      var res = data.split("=")
                      if(res.length > 2)
                      {
                        var zids = res[2].split("&");
                        var zid = zids[0]
                      }else if(res.length == 2)
                      {
                        var zid = res[1].split("%3D");
                        if(zid[1].indexOf("%26") >= 0)
                        {
                          zid = zid[1].split("%26");
                          zid = zid[0];
                        }else{
                          var zid = zid[1]
                        }
                      }else{
                        var zids = res[1].split("%26")
                        var zidss = zids[0].split("%3D");
                        var zid = zidss[1]
                        var zid = res[1];
                        zid = zid.substr(6)
                      }
                      app.util.request({
                        'url': 'entry/wxapp/zhuanjia.checkcollect',
                        data: {
                          openid: wx.getStorageSync('openid'),
                          zid: zid,
                          cerated_type: 7, //签约专家
                        },
                        success: function (res) {
                          
                          if(res.data.ifqianyue == 1)
                          {
                            wx.showToast({
                              title: '签约中，请等待审核',
                            })
                          }else if(res.data.ifqianyue == 2)
                          {
                            wx.navigateTo({
                              url: '/hyb_yl/huanzhe/pages/private_services/private_services?zid='+zid+'&id='+that.data.id+'&title='+that.data.title+'&keyword='+that.data.keyword,
                            })
                          }else if(res.data.ifqianyue == 3 || res.data.ifqianyue == 4 || res.data.ifqianyue == 5 || res.data.ifqianyue == 0)
                          {
                            wx.navigateTo({
                              url: '/hyb_yl/huanzhe/pages/patient_report/patient_report?zid='+zid,
                            })
                            
                          }else{
                            wx.navigateTo({
                              url: '/hyb_yl/huanzhe/pages/patient_report/patient_report?zid='+zid,
                            })
                          }
                          
                        },
                      })
                    }
                  })
                }else{
                  wx.navigateTo({
                    url: '/hyb_yl/mycenter/pages/followDoc/followDoc?typs=siren_doc',
                  })
                }
              }
            })
          }
        }
      })
      
    }
  },
  wenzhen(e) {
    var sid = e.detail.value.sid
    var pinyin = e.detail.value.pinyin
    var name = e.detail.value.name
    if (pinyin == 'yuanchengwenzhen') {
      wx.navigateTo({
        url: '/hyb_yl/czhuanjiasubpages/pages/longsever/index?sid=' + sid + '&key=' + pinyin + '&name=' + name,
      })
    } else if (pinyin == "kuaisuwenzhen") {
      wx.navigateTo({
        url: '/hyb_yl/userLife/pages/index/index',
      })
    } else {
      wx.navigateTo({
        url: '/hyb_yl/userCommunicate/pages/changeDoctor/changeDoctor?sid=' + sid + '&key=' + pinyin + '&name=' + name,
      })
    }

  },
  //点击专病服务下的按钮
  zbfw(e) {

    var id = e.detail.value.id
    var p_id = e.detail.value.zx_id
    var zid = e.detail.value.zid
    //console.log(id, p_id)
    //增加阅读数
    app.util.request({
      'url': 'entry/wxapp/zixun.addyuedu',
      data: {
        id: id,
      },
      success: function (res) {

      },
      fail: function (err) {
        //console.log(err)
      },
    })
    app.util.request({
      'url': 'entry/wxapp/zixun.erweima',
      data: {
        id: id,
        p_id: p_id
      },
      success: function (res) {

      },
      fail: function (err) {
        //console.log(err)
      },
    })
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunanlixq/zixunanlixq?id=' + id + '&p_id=' + p_id + '&zid=' + zid,
    })
  },
  //体检解读
  tijianjiedu(e) {
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/baogaojiedu/baogaojiedu?key_words=tijianjiedu',
    })
  },
  //手术预约
  shoushukuaiyue(e) {
    var that = this;
    var pinyin = e.currentTarget.dataset.key
    var name = e.currentTarget.dataset.name
    var sid = e.currentTarget.dataset.sid
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/shoushukuaiyue/shoushukuaiyue?pinyin=' + pinyin + '&typs=query'
    })
  },
  //家庭医生
  sirenyisheng(e) {
    var that = this
    var pinyin = e.currentTarget.dataset.key
    var name = e.currentTarget.dataset.name
    var sid = e.currentTarget.dataset.sid
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/changeDoctor/changeDoctor?sid=' + sid + '&key=' + pinyin + '&name=' + name
    })
  },
  loadInfo: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标  
      success: function (res) {

        var longitude = res.longitude
        var latitude = res.latitude
        wx.setStorageSync("latitude", latitude);
        wx.setStorageSync("longitude", longitude);
        that.loadCity(longitude, latitude)
      },

      fail: function () {
        // fail  
      },
      complete: function () {
        // complete  
      }
    })
  },

  distance: function (la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    this.setData({
      s: s
    })
  },
  loadCity: function (longitude, latitude) {
    var page = this
    var baidukey = wx.getStorageSync('baidukey')
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=' + baidukey + '&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {

        if (res.data.message) {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        } else {
          var city = res.data.result.addressComponent.city;
          wx.setStorageSync('city', city)
          //查询当前城市的所有活动
          var lat = res.data.result.location.lat
          var lng = res.data.result.location.lng
          var sematic_description = res.data.result.sematic_description
          var types = 0
          page.setData({
            position: city,
            lat: lat,
            lng: lng,
            sematic_description: sematic_description
          });
        }
      },
      fail: function () { },
      complete: function () { }
    })
  },
  hotmore() {
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/publicProblems/publicProblems?tabindex=-1',
    })
  },
  zhuanbingfuwu() {
    wx.reLaunch({
      url: '/hyb_yl/tabBar/jibingyufang/jibingyufang',
    })
  },
  hotdetail(e) {
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var typs = e.currentTarget.dataset.typs;
    var state = e.currentTarget.dataset.state
    app.util.request({
      'url': 'entry/wxapp/answer.erweima',
      data: {
        id: id,
        typs: typs,
        state: state
      },
      success: function (res) {
        wx.navigateTo({
          url: '/hyb_yl/twosubpages/pages/publicProblemsInfor/publicProblemsInfor?id=' + id + '&title=' + title + '&typs=' + typs + '&state=' + state,
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })

  },
  bindUsername: function (e) {
    this.setData({
      name: e.detail.value
    });
  },

  bindPassword: function (e) {
    this.setData({
      psd: e.detail.value
    });
  },
  onFocusPsd: function () {
    this.setData({
      psdFocus: 'psdFocus'
    })
  },
  onBlurPsd: function () {
    this.setData({
      psdFocus: ''
    })
  },
  onFocusName: function () {
    this.setData({
      nameFocus: 'nameFocus'
    })
  },
  onBlurName: function () {
    this.setData({
      nameFocus: ''
    })
  },

  login: function () {
    wx.setStorage({
      key: "myUsername",
      data: wx.getStorageSync('myUsername')
    });

    getApp().conn.open({
      apiUrl: WebIM.config.apiURL,
      user: wx.getStorageSync('myUsername'),
      pwd: '123',
      grant_type: this.data.grant_type,
      appKey: WebIM.config.appkey
    });

  },
  zhuanjiatuandui(e) {
    var that = this
    var pinyin = e.currentTarget.dataset.key
    var name = e.currentTarget.dataset.name
    var sid = e.currentTarget.dataset.sid
    wx.navigateTo({
      url: '/hyb_yl/doctor/pages/familydoctor/mydoctor/mydoctor?sid=' + sid + '&key=' + pinyin + '&name=' + name,
    })
  },
  yuanchengguahao(e) {
    var pinyin = e.currentTarget.dataset.key
    var name = e.currentTarget.dataset.name
    var sid = e.currentTarget.dataset.sid
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/longsever/index?sid=' + sid + '&key=' + pinyin + '&name=' + name,
    })
  },
  //查询当前用户是否有签约家庭医生
  getYishuo() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.userqydoc',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (result) {
        that.setData({
          doctorspeack: result.data
        })
      }
    })
  },
  yuyuejiuzhen: function (e) {
    var that = this;
    var urls = e.currentTarget.dataset.url + '?ser_key=' + e.currentTarget.dataset.key + '&name=' + e.currentTarget.dataset.name
    wx.navigateTo({
      url: urls,
    })
  },
  // 获取热门话题
  getHotAnswer() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.hotanswer',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (result) {

        that.setData({
          hotanswer: result.data
        })
      }
    })
  },
  getAddress() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.addressopen',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (result) {

        console.log(result)
        console.log(result.data)
        if (result.data) {
          var cityList = result.data
        } else {
          var cityList = []
        }

        cityList.unshift({
          parentid: '0',
          name: '全国'
        })
        that.setData({
          cityList: cityList
        })
      }
    })
  },
  // AI检测
  toequipment: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var url = e.currentTarget.dataset.url;
    var openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showModal({
        title: '提示',
        content: "请先登录",
        success: function (con) {
          if (con.confirm) {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          } else {
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    } else {
      app.util.request({
        'url': "entry/wxapp/index.is_binding",
        data: {
          type: type,
          openid: openid
        },
        success: function (res) {
          if (res.data.code == 1) {
            wx.navigateTo({
              url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=equipment&link=' + res.data.url,
            })
          } else if (res.data.code == 0) {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              success: function (con) {
                if (con.confirm) {
                  wx.navigateTo({
                    url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=equipment',
                  })
                }
              }
            })
          }
        }
      })
    }

  },
  lab_choose: function (e) {
    var index = e.currentTarget.dataset.index;
    var oid = e.currentTarget.dataset.oid;
    var screenList = this.data.screenList;
    screenList[oid].labList[index].state = !screenList[oid].labList[index].state
    this.setData({
      screenList
    })
  },
  // 筛选医生重置
  screen_reset: function (e) {
    var that = this;
    let section2 = that.data.section2;
    section2.map(function (item) {
      item.checked = false
    })
    this.setData({
      keshi: "",
      dex: "",
      zhicheng: "",
      keyword: "",
      page: 0,
      cityName: "",
      section2: section2,
      recommend_state: false,
      screen_state: false,
      screen_zhe: false,
      keshi_state: false,
      city_state: false,
    })
    this.getZhuanjia();
  },
  // 确定筛选
  screen_confirm: function (e) {
    this.setData({
      keshi_state: false,
      city_state: false,
      recommend_state: false,
      screen_state: false,
      screen_zhe: false,
      page: 0,
    })

    this.getZhuanjia()
  },
  // 筛选医院重置
  screen_resets: function (e) {
    var that = this;
    this.setData({
      cityName: "",
      dex2: "",
      pages: 0,
      level: "",
      recommend_states: false,
      screen_states: false,
      screen_zhe: false,
    })
    this.getHospital();
  },
  // 确定筛选
  screen_confirms: function (e) {
    this.setData({
      recommend_states: false,
      screen_states: false,
      screen_zhe: false,
      pages: 0,
    })

    this.getHospital()
  },
  // 医生综合推荐切换
  choose_recommend: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      recommend_state1: index,
      recommend_state: false,
      screen_state: false,
      screen_zhe: false,
      keshi_state: false,
      city_state: false,
      dex1: index,
      page: 0,
    })
    this.getZhuanjia();
  },
  // 医院综合排序
  choose_recommends: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      recommend_state1s: index,
      recommend_states: false,
      screen_states: false,
      screen_zhe: false,
      keshi_state: false,
      city_state: false,
      dex2: index,
      pages: 0,
    })
    this.getHospital();
  },
  // 切换医师/医院
  tab_choose: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      tab_state: index
    })
  },
  recommend_screen: function (e) {
    this.setData({
      recommend_state: !this.data.recommend_state,
      screen_zhe: !this.data.recommend_state,
      screen_state: false,
      ks_state: false,
      keshi_state: false,
      city_state: false,
    })
  },
  recommend_screens: function (e) {
    this.setData({
      recommend_states: !this.data.recommend_states,
      screen_zhe: !this.data.recommend_state,
      screen_states: false,
      keshi_state: false,
      city_state: false,
    })
  },
  screen_btn: function (e) {
    this.setData({
      keshi_state: false,
      city_state: false,
      recommend_state: false,
      screen_state: !this.data.screen_state,
      screen_zhe: !this.data.screen_state,
    })

  },
  screen_btns: function (e) {
    this.setData({
      keshi_state: false,
      city_state: false,
      recommend_states: false,
      screen_states: !this.data.screen_states,
      screen_zhe: !this.data.screen_state,
    })

  },
  close_screen: function () {
    this.setData({
      recommend_state: false,
      screen_state: false,
      recommend_states: false,
      screen_states: false,
      screen_zhe: false,
      ks_state: false,
      keshi_state: false,
      city_state: false,
    })
  },
  // 滚动到某处悬浮
  onPageScroll: function (e) {
    let that = this;
    // 获取医生/医院相对页面的高度
    let query = wx.createSelectorQuery()
    query.select('#hospital_box').boundingClientRect((rect) => {
      let top = rect.top;
      if (top <= -50) { //临界值，根据自己的需求来调整
        this.setData({
          fixedNav: true, //是否固定导航栏
        })
      } else {
        this.setData({
          fixedNav: false,
        })
      }
    }).exec()
  },
  // 医疗
  bindtab(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    that.setData({
      tabindex: index,
      rolldistance: 'item_' + (index - 1),
    })
  },
  bindfw_img(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    that.setData({
      tabindex: index,
      rolldistance: 'item_' + (index - 1),
    })
  },
  bindchange(e) {
    var that = this
    var index = e.detail.current

    that.setData({
      tabindex: index,
      rolldistance: 'item_' + (index - 1),
    })
  },
  // 科室筛选出现
  ks_dian: function () {
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/faxian/faxian?typs=index',
    })
  },
  // 选择科室
  departabs: function (e) {
    var that = this;
    that.setData({
      ks_state1: e.currentTarget.dataset.index,
      keshi: e.currentTarget.dataset.id,
      ks_state: false,
      doctor: [],
      page: 0,
      recommend_state: false,
      screen_state: false,
      screen_zhe: false,
    })
    that.getZhuanjia();
  },
  itemchecked1: function (e) {
    var that = this
    var server_key = e.currentTarget.dataset.server_key
    var text = e.currentTarget.dataset.text
    console.log(that.data.ktypes)

    that.setData({
      typeText: text,
      keyword: server_key,
      recommend_state: 0,
    })
  },
  // 医院选择等级
  itemchecked1s: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    that.setData({
      level: id,
      recommend_states: 0,
    })
  },
  typesclick1: function (e) {
    var that = this
    let datas = that.data;
    let i = e.currentTarget.dataset.index
    let dex = e.currentTarget.dataset.dex
    datas.section2.map(function (item) {
      item.checked = false
    })
    datas.section2[dex].checked = true
    that.setData({
      section2: datas.section2,
      yishengText: datas.section2[dex].job_name,
      id: datas.section2[dex].id,
      zhicheng: e.currentTarget.dataset.id
    })
  },
  getZhuanjia: function () {
    var that = this;
    console.log(that.data.parentid);
    app.util.request({
      'url': "entry/wxapp/index.zhuanjia",
      data: {
        dex: that.data.dex,
        page: that.data.page,
        zhicheng: that.data.zhicheng,
        parentid: that.data.parentid,
        keyword: that.data.keyword,
        city: that.data.cityName
      },
      success: function (res) {
        if (that.data.page == 0) {
          that.setData({
            doctor: res.data,
            isLoading: !1
          })
        } else {
          that.setData({
            doctor: that.data.doctor.concat(res.data),
            isLoading: !1
          })
        }
        var page = that.data.page + 1;
        that.setData({
          page: page
        })
        console.log(that.data.doctor)
      }
    })
  },


  getKeshi_one: function () {
    var that = this;
    app.util.request({
      url: "entry/wxapp/index.address",
      data: {
        op: "keshi"
      },
      success: function (res) {
        that.setData({
          one_arr: res.data,
          keshi_one: res.data[0].id
        })
        that.getKeshi_two();
      }
    })
  },
  getKeshi_two: function (res) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/index.parentclass",
      data: {
        id: that.data.keshi_one
      },
      success: function (res) {
        that.setData({
          two_list: res.data,
        })
      }
    })
  },
  getHotcity: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/zhuanjia.hot_address",
      success: function (res) {
        var citys = [];
        for (var i = 0; i < res.data.length; i++) {
          citys.push(res.data[i].name)
        }
        tcity.init(that);
        var cityData = that.data.cityData;
        const provinces = [];
        provinces.push("热门城市");
        for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
        }
        console.log(provinces);
        console.log(citys);
        that.setData({
          provinces,
          citys,
        })
      }
    })
  },
  //城市
  cityitem(e) {
    var val = e.currentTarget.dataset.dex
    var name = e.currentTarget.dataset.name
    this.setData({
      citysdex: val,
      popshow: false,
      tabdex: "",
      page: 0,
      hospitallm: [],
      city: name,
      cityName: name,
      pages: 0,
      list: []
    })
    if (this.data.tab_state == 1) {
      this.getZhuanjia();
    } else {
      this.getHospital();
    }
    this.close_screen()
  },
  //选择省份
  provinceitem(e) {
    var val = e.currentTarget.dataset.dex
    var t = this.data.provincesdex;
    var cityData = this.data.cityData;
    if (val != t) {
      if (val > 0) {
        let ci = val - 1
        let citys = [];
        for (let i = 0; i < cityData[ci].sub.length; i++) {
          citys.push(cityData[ci].sub[i].name)
        }
        this.setData({
          citys,
          provincesdex: val
        })
      } else if (val == 0) {
        let citys = ["全国", "北京", "上海", "广州", "杭州", "西安", "武汉"];
        this.setData({
          citys,
          provincesdex: val
        })
      }
    }
  },
  bindadd:function(e){
    var type = e.currentTarget.dataset.type;
    var that = this;
    
    if(type == 'blood_pressure')
    {
      wx.showModal({
        title: '提示',
        content:"请使用血压仪测量，数据会自动上传,是否查看",
        success:function(con)
        {
          if(con.confirm)
          {
            wx.navigateTo({
              url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?type=equipment&link=/hyb_yl/zhuanjiasubpages/pages/bloodpressure_report/bloodpressure_report',
            })
          }
        }
      })
    }else if(type == 'blood_sugar' || type == 'uric_acid' || type == 'heart_rate')
    {
      wx.openBluetoothAdapter({//调用微信小程序api 打开蓝牙适配器接口
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '初始化成功',
            icon: 'success',
            duration: 800
          })
          that.findBlues();//2.0
        },
        fail: function (res) {//如果手机上的蓝牙没有打开，可以提醒用户
          console.log(333)
          wx.showModal({
            title: '提示',
            content: "手机蓝牙未开启，仅可查看记录",
            success: function (con) {
              if (con.confirm) {
                if(type == 'blood_sugar')
                {
                  var url = '/hyb_yl/zhuanjiasubpages/pages/bloodsugar_report/bloodsugar_report';
                }else if(type == 'uric_acid')
                {
                  var url = "/hyb_yl/pharmacy/pages/uric_acid/uric_acid";
                }else if(type == 'heart_rate')
                {
                  var url = "/hyb_yl/pharmacy/pages/heart_rate_cont/heart_rate_cont";
                }
                that.setData({
                  url:url
                })
                wx.navigateTo({
                  url: url + '?j_id=' + that.data.j_id,
                })
              }
            }
          })
        }
      })
    }else if(type == 'temperature')
    {
      wx.openBluetoothAdapter({
        success: function (res) {
          that.Search();
        },
        fail: function (res) {
          wx.showModal({
            title: '提示',
            content: "手机蓝牙未开启，仅可查看记录",
            success: function (con) {
              if (con.confirm) {
                wx.navigateTo({
                  url: url + '?j_id=' + that.data.j_id,
                })
              }
            }
          })
        }
      })
    }
  },
  findBlues() {
    var that = this
    wx.startBluetoothDevicesDiscovery({
      allowDuplicatesKey: false,
      interval: 0,
      success: function (res) {
        console.log(res);
        wx.showLoading({
          title: '正在搜索设备',
        })
        that.getBlue()//3.0
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  getBlue() {
    var that = this
    var url = that.data.url;
    var type = '';
    
    if (url == '/hyb_yl/pharmacy/pages/uric_acid/uric_acid') {
      type = 'UA';
    } else if (url == '/hyb_yl/pharmacy/pages/cholesterol/cholesterol') {
      type = 'CHOL';
    } else if (url == '/hyb_yl/zhuanjiasubpages/pages/bloodsugar_report/bloodsugar_report') {
      type = 'GLU';
    }
    wx.getBluetoothDevices({
      success: function (res) {
        console.log(res)
        wx.hideLoading();
        for (var i = 0; i < res.devices.length; i++) {
          //that.data.inputValue：表示的是需要连接的蓝牙设备ID，简单点来说就是我想要连接这个蓝牙设备，所以我去遍历我搜索到的蓝牙设备中是否有这个ID
          if (res.devices[i].name.indexOf('PM10') > -1 || res.devices[i].localName.indexOf('PM10') > -1 || res.devices[i].name.indexOf('Device') > -1 || res.devices[i].localName.indexOf('Device') > -1) {
            that.setData({
              deviceId: res.devices[i].deviceId,
              consoleLog: "设备：" + res.devices[i].deviceId,
            })
            
            wx.redirectTo({ url: '/hyb_yl/pharmacy/pages/thermometer/index?connectedDeviceId=' + res.devices[i].deviceId + '&name=' + res.devices[i].name + '&j_id=' + that.data.j_id + '&type=' + type });
            return;
          }
        }
      },
      fail: function () {
        console.log("搜索蓝牙设备失败")
      }
    })
  },
  // 蓝牙设备检测数据体温心电
  Search() {
    var that = this;
    wx.startBluetoothDevicesDiscovery({
      success(res) {
        wx.showLoading({
          title: '正在搜索设备...',
        });

        var delayTimer = setInterval(function () {
          console.log('sousuo');
          that.findBlue();
        }, 2000);
        that.setData({
          delayTimer: delayTimer
        })
      },
      fail() {
        that.setData({
          showText: "搜索设备失败"
        })
        that.closeFindBlue();
      }
    })

  },
  findBlue() {
    var that = this;
    console.log('搜索开始')
    wx.getBluetoothDevices({
      success: function (res) {
        console.log('搜索成功')
        console.log(res)
        var deviceNum = 0;
        if (res.devices && (deviceNum = res.devices.length) >= 1) {
          that.setData({
            showText: "搜索设备数量：" + deviceNum
          })

          that.setData({
            devicesList: []

          });

          var biolandDevice;
          for (var i = 0; i < deviceNum; i++) {
            var device = res.devices[i];
            var deviceName = device.name;
            if (deviceName == null || deviceName == '')
              continue;

            var rssi = device.RSSI;
            if (deviceName.indexOf('Bioland-IT') > -1) {
              biolandDevice = device;
              var name = "devicesList[" + i + "].name";
              that.setData({
                [name]: deviceName
              });
              break;
            } else if (deviceName.indexOf('PM10') > -1) {
              biolandDevice = device;
              var name = "devicesList[" + i + "].name";
              that.setData({
                [name]: deviceName
              });
              break;
            } else if (deviceName.indexOf('BLE') > -1) {
              biolandDevice = device;
              var name = "devicesList[" + i + "].name";
              that.setData({
                [name]: deviceName
              });
              break;
            }
          }
          console.log(biolandDevice)
          if (biolandDevice != null) {
            wx.hideLoading();
            that.closeFindBlue();
            that.Connect(biolandDevice);
          }

        }
      }
    })
  },
  closeFindBlue() {
    var that = this;
    clearInterval(that.data.delayTimer);
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) {
      }
    })
  },
  Connect(data) {
    if (data == null)
      return;
    var that = this
    wx.showLoading({
      title: '连接设备中...',
    })
    wx.createBLEConnection({
      deviceId: data.deviceId,
      success: function (res) {
        wx.hideLoading()
        console.log(that.data.j_id);

        wx.redirectTo({ url: '/hyb_yl/pharmacy/pages/thermometer/index?connectedDeviceId=' + data.deviceId + '&name=' + data.name + '&j_id=' + that.data.j_id + '&type=tem' });
      },
      fail: function (res) {
        wx.hideLoading()
      }
    })
  },
})